package com.yxq.mdt.app.modules.personal.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.personal.adapter.PersonalBrowsingHistoryListAdapter;
import com.yxq.mdt.app.modules.personal.bean.FootBean;
import com.yxq.mdt.app.modules.personal.bean.RecordUserListBean;
import com.yxq.mdt.app.modules.personal.model.FootModel;
import com.yxq.mdt.app.modules.personal.presenter.FootPresenter;
import com.yxq.mdt.app.modules.personal.presenter.IFootContract;

import butterknife.BindView;
import butterknife.ButterKnife;

/***
 * 谁看过我
 */
public class MainPersonalBrowsingHistoryAc extends BaseActivity<FootPresenter> implements IFootContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    PersonalBrowsingHistoryListAdapter personalBrowsingHistoryListAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_all_number)
    TextView tvAllNumber;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_personal_browsing_history;
    }

    @Override
    public void initView() {
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.selectMyViewRecordVoList(1, 20, 0, 0, null, 0);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.selectMyViewRecordVoList(page, 20, 0, 0, null, 0);
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        personalBrowsingHistoryListAdapter = new PersonalBrowsingHistoryListAdapter(R.layout.item_personal_browsing_history_layout, null);
        recyclerView.setAdapter(personalBrowsingHistoryListAdapter);
        personalBrowsingHistoryListAdapter.setOnItemClickListener((adapter, view1, position) -> {
            Intent intent = new Intent(mActivity, MainPersonalOtherFootprintAc.class);
            intent.putExtra("data", JSON.toJSONString(personalBrowsingHistoryListAdapter.getItem(position)));
            startActivity(intent);
        });
        personalBrowsingHistoryListAdapter.setEmptyView(R.layout.layout_empty_view, recyclerView);
        personalBrowsingHistoryListAdapter.isUseEmpty(false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new FootPresenter(this, new FootModel());
        refreshLayout.autoRefresh();
    }

    @Override
    public void onLoadSuccess(FootBean footBean) {

    }

    @Override
    public void onLoadFailed(String msg) {

    }

    @Override
    public void onLoadUserListSuccess(RecordUserListBean recordUserListBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        tvAllNumber.setText(String.format("看过我的用户(%d)", recordUserListBean.getTotal()));
        if (page == 1) {
            personalBrowsingHistoryListAdapter.setNewData(recordUserListBean.getList());
        } else {
            if (recordUserListBean.getList() != null && recordUserListBean.getList().size() != 0) {
                personalBrowsingHistoryListAdapter.addData(recordUserListBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        personalBrowsingHistoryListAdapter.isUseEmpty(!(personalBrowsingHistoryListAdapter.getData().size() > 0));
    }

    @Override
    public void onLoadUserListFailed(String msg) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }
}
